//
//  BarcodeInputCell.swift
//  Wmsmobile
//
//  Created by Nick xu on 16/8/5.
//  Copyright © 2016年 hddden. All rights reserved.
//

import UIKit

let inputViewToCellSpace:CGFloat = 100
let FTCellTopSpace:CGFloat = 5
let FTCellHorizontalSpace:CGFloat = 10

class BarcodeInputCell: FounctionBaseCell, inputViewProtocol , UITextFieldDelegate , displayDetailProtocol{
    //MARK: - property
    override var info: [String : String]?{
        didSet{
            if info!["button_cameraHiden"] != nil{
                button_cameraHiden = true
            }else{
                button_cameraHiden = false
            }
            if let newKey = info!["keyToDisplay"] {
                dispalyKey = newKey
            }
        }
    }
     var button_cameraHiden:Bool?
    var dispalyKey: String?
    var keyToDisplay: String?{
        set{
            dispalyKey = newValue
        }
        get{
            return dispalyKey
        }
    }
    lazy var codeInputView: WmInputView = {
        let view = WmInputView()
        view.textField.delegate = self
        return view
    }()
    var inputState:inputViewState = inputViewState.disabled{
        didSet{
            switch inputState { //改变状态
            case .activated:
//                codeInputView.layer.borderColor = UIColor.hexStringToUIColor("#0ebfb1").CGColor
                codeInputView.userInteractionEnabled = true
                codeInputView.textField.inputView = customTextfield.emptyView
                codeInputView.textField.resignFirstResponder()
                if codeInputView.textField.canBecomeFirstResponder(){
                    codeInputView.textField.becomeFirstResponder()
                }
                self.updateButtonState()
                //                codeInputView.button_camera.hidden = button_cameraHiden ?? false
                CaptuvoDecoderManager.shareCaptuvoDecoderManager.delegate = self
                AVQRCodeViewManager.shareAVQRCodeViewManager.delegate = self
            case .disabled:
//                codeInputView.layer.borderColor = UIColor.hexStringToUIColor("#bbbbbb").CGColor
                codeInputView.userInteractionEnabled = false
                codeInputView.button_camera.hidden = true
                codeInputView.button_clear.hidden = true
            case .wrong:
<<<<<<< HEAD
                self.updateButtonState()
                codeInputView.layer.borderColor = UIColor.redColor().CGColor
=======
//                codeInputView.layer.borderColor = UIColor.redColor().CGColor
>>>>>>> wsn
                codeInputView.userInteractionEnabled = true
                codeInputView.textField.becomeFirstResponder()
                //                codeInputView.button_camera.hidden = button_cameraHiden ?? false
            }

    }
    }
    var state:inputViewState?{
        get{
            return inputState
        }
        set{
            inputState = newValue!
        }
        
        
    }
    weak var delegateReal:normalInputViewStateProtocol?
    weak var delegate:normalInputViewStateProtocol?{
        set{
            self.delegateReal = newValue
        }
        get{
            return delegateReal
        }
    }
    //MARK: - life cycle
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: UITableViewCellStyle.Default, reuseIdentifier: "BarcodeInputCell")
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.setUpUI()
    }
    
    required init() {
        super.init(style: UITableViewCellStyle.Default, reuseIdentifier: "BarcodeInputCell")
        self.setUpUI()
    }
    //MARK: - public
    override func clearContent() {
        self.codeInputView.textField.text = ""
    }
    
    //MARK: - textFieldDelegate
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        self.finishInput(codeInputView.textField.text)
        return true
    }
    //MARK: - delegate
    func textChanged(notification: NSNotification){
        self.updateButtonState()
        if self.delegate != nil{
            let textField = notification.object as? UITextField
            self.delegate?.updateDetail(textField!.text!, forKey: (info!["keyToUpdate"]?.componentsSeparatedByString(",").first)!)
        }
    }
    func finishInput(text: String?) {
        if let delegate = self.delegate{
            self.delegate?.updateDetail(text ?? "", forKey: (info!["keyToUpdate"]?.componentsSeparatedByString(",").first)!)
            delegate.finishInput(self)
        }
    }
    func setDetail(string: String) {0
        self.codeInputView.textField.text = string
        if self.delegate != nil{
            self.delegate?.updateDetail(string, forKey: (info!["keyToUpdate"]?.componentsSeparatedByString(",").first)!)
        }
    }
    func isEmpty() -> (Bool) {
        return self.codeInputView.textField.text?.characters.count > 0 ? false : true
    }
    //MARK: - private
    func updateButtonState(){
        if button_cameraHiden! {
            
        }else{
            codeInputView.button_camera.hidden = codeInputView.textField.text?.characters.count > 0 ? true : false
        }
        codeInputView.button_clear.hidden = codeInputView.textField.text?.characters.count > 0 ? false : true
    }
    func setUpUI(){
         codeInputView.button_camera.hidden = button_cameraHiden ?? false
        self.state = inputViewState.disabled
        self.addSubview(self.codeInputView)
//        codeInputView.tintColor = UIColor.hexStringToUIColor("#f01d3c")
        codeInputView.snp_makeConstraints { (make) in
            make.leading.equalTo(self).offset(inputViewToCellSpace+20)
            make.top.equalTo(self).offset(FTCellTopSpace+4)
            make.bottom.equalTo(self).offset(-FTCellTopSpace-5)
            make.trailing.equalTo(self).offset(-9)
        }
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(BarcodeInputCell.textChanged(_:)), name: UITextFieldTextDidChangeNotification, object: self.codeInputView.textField)
//        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(SwitchCell.finishInput), name: "finishScanCode", object: nil)//注册通知
    }
  
    
}
